package com.dyzx.hbb.modules.sys.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dyzx.hbb.modules.sys.entity.SysNotice;
import com.dyzx.hbb.modules.sys.entity.SysNoticeRead;
import com.dyzx.hbb.modules.sys.entity.SysUser;
import com.dyzx.hbb.modules.sys.mapper.SysNoticeMapper;
import com.dyzx.hbb.modules.sys.service.SysNoticeReadService;
import com.dyzx.hbb.modules.sys.service.SysNoticeService;
import com.dyzx.hbb.modules.sys.service.SysUserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author Administrator
 * @description 针对表【sys_notice(创建通知表)】的数据库操作Service实现
 * @createDate 2025-04-22 17:48:54
 */
@Service
public class SysNoticeServiceImpl extends ServiceImpl<SysNoticeMapper, SysNotice>
        implements SysNoticeService {
    @Autowired
    private SysUserService userService;
    @Autowired
    private SysNoticeReadService noticeReadService;


    @Override
    public boolean sendNotice(SysNotice entity) {
/*        recipient_range	int	是	二级	接收范围
        字典信息：sys_notice_range
        1：全部，2：指定机构，3：指定部门 ，4：指定用户*/

        Integer recipientRange = entity.getRecipientRange();
        switch (recipientRange) {
            case 1:
                // 发送给所有用户
                return sendNoticeToAll(entity);
            case 2:
                // 发送给指定机构用户
                return sendNoticeToOrgan(entity);
            case 3:
                // 发送给指定部门用户
                return sendNoticeToDept(entity);
            case 4:
                // 发送给指定用户
                List<Long> userIds = Arrays.stream(entity.getRecipientId().split(",")).map(Long::parseLong).toList();
                return sendNoticeToUsers(entity, userIds);
            default:
                return false;
        }
    }

    private boolean sendNoticeToUsers(SysNotice entity, List<Long> userIds) {
        // 获取指定用户列表
        if (userIds == null || userIds.isEmpty()) {
            return false;
        }
        entity.setId(null);
        // 保存通知
        boolean saved = this.save(entity);
        if (saved) {
            // 批量创建通知阅读记录
            List<SysNoticeRead> readList = userIds.stream()
                    .map(userId -> {
                        SysNoticeRead read = new SysNoticeRead();
                        read.setNoticeId(entity.getId());
                        read.setOperatorId(userId);
                        read.setOrganId(entity.getOrganId());
                        read.setNoticeId(entity.getId());
                        read.setCreateDate(System.currentTimeMillis());
                        return read;
                    })
                    .collect(Collectors.toList());
            // 批量保存通知阅读记录
            return noticeReadService.saveBatch(readList);
        }
        return false;


    }

    private boolean sendNoticeToDept(SysNotice entity) {
        // 获取部门ID
        String deptIds = entity.getRecipientId();
        if (StringUtils.isEmpty(deptIds)) {
            return false;
        }
        String[] split = deptIds.split(",");
        List<Long> userIds = userService.findByDeptIds(Arrays.asList(split));

        // 保存通知
        return sendNoticeToUsers(entity, userIds);
    }

    private boolean sendNoticeToOrgan(SysNotice entity) {
        // 获取机构ID
        String organIds = entity.getRecipientId();
        if (StringUtils.isEmpty(organIds)) {
            return false;
        }
        String[] split = organIds.split(",");
        List<Long> userIds = userService.findByOrganIds(Arrays.asList(split));

        // 保存通知
        return sendNoticeToUsers(entity, userIds);
    }

    private boolean sendNoticeToAll(SysNotice entity) {
        // 发送给所有用户，直接保存通知
        List<Long> userIds = userService.list().stream().map(SysUser::getId).toList();
        return sendNoticeToUsers(entity, userIds);
    }
}




